<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/6/12
     * Time: 下午8:54
     */

    namespace app\store\admin;

    use app\admin\controller\Admin;
    use app\common\builder\ZBuilder;
    use app\store\model\GoodsModel;
    use app\store\model\CategoryModel;
    use app\wechat\api\Member;

    class Goods extends Admin
    {

        /**
         * @return mixed
         * @throws \think\Exception
         * @throws \think\exception\DbException
         */
        public function lists()
        {

            // 查询
            $map = $this->getMap();
            // 排序
            $order = $this->getOrder('id desc');

            $data_list = GoodsModel::where($map)->order($order)->paginate();

            return ZBuilder::make('table')->setPageTitle('商品管理')->setSearch(['name' => '商品名称'])// 设置搜索框
            ->addColumns([ // 批量添加数据列
                           ['id','ID'],
                           ['name','商品名称','text'],
                           //['storeclass_id', '商品分类', 'text'],
                           ['cid','分类名称','',CategoryModel::where(['show' => 1])->column('id,name')],
                           ['image','广告图','picture'],
                           ['price','商品价格','text'],
                           ['point','兑换积分','text'],
                           ['num','库存','text'],
                           ['click','浏览数','text'],
                           ['collect_num','收藏数','text'],
                           ['commend','推荐','text'],
                           ['order','排序','text'],
                           ['salenum','销售数量','text'],
                           ['status','是否上架','yesno'],
                           ['right_button','操作','btn']
            ])->addTopButtons(['add','enable','disable','delete'])// 批量添加顶部按钮
            ->addRightButtons(['edit','delete' => ['data-tips' => '删除后无法恢复。']])// 批量添加右侧按钮
            ->addOrder('id,name,cid,price,point,click,num,collect_num,commend,order,salenum,status')//

            ->setRowList($data_list)// 设置表格数据
            ->fetch(); // 渲染模板
        }

        /**
         * 新增
         *
         * @return mixed
         * @throws Exception
         */
        public function add()
        {

            // 保存数据
            if ($this->request->isPost())
            {
                // 表单数据
                $data = $this->request->post();

                if (isset($data['feature']))
                {
                    $data['feature'] = join(',',$data['feature']);
                }
                if ($GoodsModel = GoodsModel::create($data))
                {
                    // 记录行为
                    //action_log('GoodsModel_add', null, $GoodsModel['id'], session('admin_id'), $GoodsModel['id']);
                    $this->success('新增成功','lists');
                }
                else
                {
                    $this->error('新增失败');
                }
            }

            // 显示添加页面
            return ZBuilder::make('form')->setBtnTitle('back','返回前一页')->addFormItems([
                ['text','name','商品名称'],
                ['select','cid','分类名称','',CategoryModel::where(['show' => 1])->column('id,name')],
                ['text','order','排序'],
                ['text','price','商品价格'],

                ['text','point','兑换积分'],

                ['text','num','库存'],
                ['text','company','单位'],
                ['radio','commend','是否推荐','',['否','是'],1],
                ['radio','status','立即启用','',['否','是'],1],
                ['image','image','广告图'],
                ['images','images','轮播图'],
                ['ueditor','body','商品详情描述'],
            ])->fetch();
        }

        /**
         * 编辑
         *
         * @param null $id
         *
         * @return mixed
         * @throws Exception
         * @throws \think\exception\DbException
         */
        public function edit($id = null)
        {

            if ($id === null)
                $this->error('缺少参数');

            // 保存数据
            if ($this->request->isPost())
            {
                // 表单数据
                $data = $this->request->post();

                if (GoodsModel::update($data,['id' => $data['id']]))
                {
                    $this->success('编辑成功','lists');
                }
                else
                {
                    $this->error('编辑失败');
                }
            }

            $info = GoodsModel::field('*')->where(['id' => $id])->find();

            // 显示编辑页面
            return ZBuilder::make('form')->addFormItems([
                ['hidden','id'],
                ['text','name','商品名称'],
                ['select','cid','分类名称','',CategoryModel::where(['show' => 1])->column('id,name')],
                ['text','order','排序'],
                ['text','price','商品价格'],
                ['text','point','兑换积分'],

                ['text','num','库存'],
                ['text','company','单位'],
                ['radio','commend','是否推荐','',['否','是'],1],
                ['radio','status','立即启用','',['否','是'],1],
                ['image','image','广告图'],
                ['images','images','轮播图'],
                ['ueditor','body','商品详情描述'],
            ])->setFormData($info)->fetch();
        }

        /**
         * 删除
         *
         * @param array $record
         *
         * @return mixed
         */
        public function delete($record = [])
        {

            return $this->setStatus('delete',new GoodsModel);
        }

        /**
         * 启用
         *
         * @param array $record
         *
         * @return mixed
         */
        public function enable($record = [])
        {

            return $this->setStatus('enable',new GoodsModel);
        }

        /**
         * 禁用
         *
         * @param array $record
         *
         * @return mixed
         */
        public function disable($record = [])
        {

            return $this->setStatus('disable',new GoodsModel);
        }

        /**
         * 设置状态：删除、禁用、启用
         *
         * @param string $type
         * @param array  $record
         * @param null   $Model
         *
         * @return mixed
         */
        public function setStatus($type = '',$record = [],$Model = null)
        {

            $ids        = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
            $link_title = GoodsModel::where('goods_id','in',$ids)->column('goods_name');
            if ($Model)
            {
                return parent::setStatus($type,[
                    'GoodsModel_' . $type,
                    null,
                    0,
                    session('admin_id'),
                    implode('、',$link_title)
                ],$Model);
            }
            else
            {
                return parent::setStatus($type,[
                    'GoodsModel_' . $type,
                    null,
                    0,
                    session('admin_id'),
                    implode('、',$link_title)
                ],new GoodsModel);

            }
        }

        /**
         * 快速编辑
         *
         * @param array $record
         * @param null  $Model
         */
        public function quickEdit($record = [],$Model = null)
        {

            $id         = input('post.pk','');
            $field      = input('post.name','');
            $value      = input('post.value','');
            $GoodsModel = GoodsModel::where('goods_id',$id)->value($field);
            $details    = '字段(' . $field . ')，原值(' . $GoodsModel . ')，新值：(' . $value . ')';
            return parent::quickEdit(['link_edit','cms_link',$id,session('admin_id'),$details],new GoodsModel());
        }

        //        public function orderedit($record = [])
        //        {
        //
        //        }
    }